home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / pcscheme / geneva / sources.exe / SOURCES / FONTS / DFONT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-12  |  6.3 KB  |  215 lines

  1.  
  2. /*
  3.  
  4.   DFONT - Dump Font file program
  5.  
  6.   Copyright (c)  1988,89 Borland International
  7.  
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14.  
  15. #include "font.h"
  16.  
  17. FILE    *ffile;
  18.  
  19. char           *Font;            /* Pointer to font storage    */
  20. char        Prefix[Prefix_Size];    /* File Prefix Holder        */
  21. HEADER        Header;         /* File Data Header        */
  22.  
  23. int        Offset[256];        /* Font data offsets        */
  24. char        Char_Width[256];    /* Character Width Table    */
  25. int        Stroke_Count[256];    /* Stroke Count Table        */
  26. STROKE           *Strokes[256];        /* Stroke Data Base        */
  27.  
  28. char  *OpName[] = {
  29.   "End",
  30.   "Scan",
  31.   "Moveto",
  32.   "Lineto"
  33.   };
  34.  
  35. char help[] = "Dump Font File Copyright (c) 1987,1989 Borland International, Inc.\n\n"
  36.           "Usage is:  DFONT [font file name] \n";
  37.  
  38.  
  39.  
  40. int unpack( char *buf, int index, STROKE **new );
  41. int decode( unsigned int *iptr, int *x, int *y );
  42.  
  43. void main( int argc, char **argv )
  44. {
  45.   long length, current, base;
  46.   char *cptr;
  47.   FHEADER *fptr;
  48.   int last_chr, i, j;
  49.   STROKE *sptr;
  50.  
  51.   if( argc != 2 ){        /* Invalid 3 of arguments    */
  52.     fprintf( stderr, help);
  53.     exit( 1 );                /* Exit the program        */
  54.     }
  55.  
  56.   ffile = fopen( argv[1], "rb");       /* Open the input font file     */
  57.   if( NULL == ffile ){            /* Can not open font file    */
  58.     fprintf( stderr, "\nFontDisplay: Can not open input file %s.\n", argv[1] );
  59.     exit( 1 );                /* Exit the program        */
  60.     }
  61.  
  62. /*    Read in and display the file prefix record.            */
  63.  
  64.   base = ftell( ffile );        /* Remember the address of table*/
  65.   fread(Prefix, Prefix_Size, 1, ffile); /* Read in the file prefix    */
  66.  
  67.   printf("Prefix Record. File Base: %lx\n\n", base );
  68.  
  69.   cptr = Prefix;            /* Begin at base of prefix    */
  70.   while( 0x1a != *cptr ) ++cptr;    /* Move to EOF in prefix    */
  71.   *cptr = '\0';                         /* Cut prefix at EOF            */
  72.   fptr = (FHEADER *)(cptr+1);        /* Point at Font Header Record    */
  73.  
  74.   printf("%s\n", Prefix );
  75.   printf("Prefix Size: %02x, Prefix Name: %-.4s, Font Size %02x\n",
  76.     fptr->header_size, fptr->font_name, fptr->font_size );
  77.   printf("Revision: %d.%d (BGI version %d.%d)\n",
  78.     fptr->font_major, fptr->font_minor,
  79.     fptr->min_major, fptr->min_minor );
  80.  
  81. /*    Read in and display the font header record.            */
  82.  
  83.   base = ftell( ffile );        /* Remember the address of table*/
  84.   fread(&Header, sizeof(HEADER), 1, ffile);  /* Read in the file prefix */
  85.  
  86.   printf("\nHeader Record. File Base: %lx, Header Name: %-.4s\n",
  87.     base, Header.fntname );
  88.   printf("Signature: %c\n", Header.sig );
  89.   printf("# Characters: %02x, First Char: %02x\n",
  90.     Header.nchrs, Header.first );
  91.   printf("Definition Offset: %04x\n", Header.cdefs );
  92.   printf("Scanable Font: %s\n", Header.scan_flag ? "Yes" : "No");
  93.   printf("Origin to Cap Height: %d; to Base Height: %d; to Dec Height: %d\n",
  94.     Header.org_to_cap, Header.org_to_base, Header.org_to_dec );
  95.  
  96. /*    Read file offset table into memory.                */
  97.  
  98.   base = ftell( ffile );        /* Remember the address of table*/
  99.   fread( &Offset[Header.first], Header.nchrs, sizeof(int), ffile );
  100.  
  101. /*    Display the offset table                    */
  102.  
  103.   last_chr = Header.first + Header.nchrs;
  104.  
  105.   printf("\nOffset Table. File Base: %lx\n", base );
  106.  
  107.   for( i=Header.first ; i<last_chr ; ++i ){
  108.     if( !((i+3) % 4) ) printf("\n");
  109.     printf(" %c (%02x) : %04x ",
  110.             i >= ' ' ? i : '.', i, Offset[i] );
  111.     }
  112.  
  113. /*    Load the character width table into memory.            */
  114.  
  115.   base = ftell( ffile );
  116.   fread( &Char_Width[Header.first], Header.nchrs, sizeof(char), ffile );
  117.  
  118. /*    Determine the length of the stroke database.            */
  119.  
  120.   current = ftell( ffile );        /* Current file location    */
  121.   fseek( ffile, 0, SEEK_END );        /* Go to the end of the file    */
  122.   length = ftell( ffile );        /* Get the file length        */
  123.   fseek( ffile, current, SEEK_SET );    /* Restore old file location    */
  124.  
  125. /*    Load the stroke database.                    */
  126.  
  127.   Font = malloc( (int) length );    /* Create space for font data    */
  128.   if( NULL == Font ){            /* Was there enough memory    */
  129.     fprintf( stderr, "FontDisplay: Not Enough Memory to load Font.\n\n");
  130.     exit( 1 );
  131.     }
  132.  
  133.   fread( Font, (int)length, 1 , ffile ); /* Load the stroke data    */
  134.  
  135. /*    Font is now loaded, display the internal data            */
  136.  
  137.   printf("\n\nWidth Table File Base: %lx; Stroke Table Base: %lx\n",
  138.     base, current );
  139.   printf("\nStroke Information\n");
  140.  
  141.   for( i=Header.first ; i<last_chr ; ++i ){
  142.     if( !Offset[i] && i!=Header.first ) continue;
  143.     printf("Char %02x (%c): ", i, i );
  144.     Stroke_Count[i] = unpack( Font, Offset[i], &Strokes[i] );
  145.     printf("Offset: %04x   Width: %-5d   Stroke Count: %d\n",
  146.       Offset[i], Char_Width[i], Stroke_Count[i] );
  147.     sptr = Strokes[i];
  148.     for( j=0 ; j<Stroke_Count[i] ; ++j, ++sptr ){
  149.       printf("\t%s(%d,%d)", OpName[sptr->opcode], sptr->x, sptr->y );
  150.       if( j % 4 == 3 && j != Stroke_Count[i]-1 ) printf("\n");
  151.       }
  152.       printf("\n");
  153.     }
  154. }
  155.  
  156. /*    UNPACK: This routine decodes the file format into a more    */
  157. /*    reasonable internal structure                    */
  158.  
  159. int unpack( char *buf, int index, STROKE **new )    /* FUNCTION    */
  160. {
  161.   unsigned int *pb;
  162.   STROKE *po;
  163.   int      num_ops = 0;
  164.   int      jx, jy, opcode, i, opc;
  165.  
  166.   pb = (unsigned int *)(buf + index);    /* Reset pointer to buffer    */
  167.  
  168.   while( FOREVER ){            /* For each byte in buffer    */
  169.     num_ops += 1;            /* Count the operation        */
  170.     opcode = decode( pb++, &jx, &jy );    /* Decode the data record    */
  171.     if( opcode == END_OF_CHAR ) break;    /* Exit loop at end of char    */
  172.     }
  173.  
  174.   po = *new = calloc( num_ops, sizeof(STROKE) );
  175.  
  176.   if( !po ){                /* Out of memory loading font    */
  177.     fprintf( stderr, "DMPFNT: Out of memory decoding font\n");
  178.     exit( 100 );
  179.     }
  180.  
  181.   pb = (unsigned int *)(buf + index);    /* Reset pointer to buffer    */
  182.  
  183.   for( i=0 ; i<num_ops ; ++i ){     /* For each opcode in buffer    */
  184.     opc = decode(pb++, &po->x, &po->y); /* Decode the data field    */
  185.     po->opcode = opc;            /* Save the opcode        */
  186.     po++;
  187.     }
  188.  
  189.   return( num_ops );            /* return OPS count        */
  190.  
  191. }
  192.  
  193.  
  194. /*    DECODE: This routine decodes a single word in file to a     */
  195. /*    stroke record.                            */
  196.  
  197. int decode( unsigned int *iptr, int *x, int *y )  /* FUNCTION        */
  198. {
  199.   struct DECODE {
  200.     signed   int xoff  : 7;
  201.     unsigned int flag1 : 1;
  202.     signed   int yoff  : 7;
  203.     unsigned int flag2 : 1;
  204.   } cword;
  205.  
  206.   cword = *(struct DECODE *)iptr;
  207.  
  208.   *x = cword.xoff;
  209.   *y = cword.yoff;
  210.  
  211.   return( (cword.flag1 << 1) + cword.flag2 );
  212.  
  213. }
  214.  
  215.